package com.ideabove.heart.game;


import java.util.ArrayList;


import android.app.Activity;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.graphics.Canvas;
import android.media.MediaPlayer;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;

import com.ideabove.heart.MainActivity;
import com.ideabove.heart.MoeActivity;
import com.ideabove.heart.data.BitmapObj;
import com.ideabove.heart.data.Comment;
import com.ideabove.heart.data.Episode;
import com.ideabove.heart.game.character.BasicGirl;
import com.ideabove.heart.game.framework.GameView;
import com.ideabove.heart.manager.MoeManager;
import com.ideabove.heart.manager.ScheduleManager;
import com.ideabove.heart.util.MyUtil;
import com.ideabove.heart.util.MyUtil.ImageTouch;

public class StoryGirlState extends MoeState{

	MediaPlayer backgroundMp;	

	BasicGirl character;
	BasicGirl lastCharatcer;
	ArrayList<BasicGirl> situationArray;
	int curStateIdx = 0;
	BitmapObj btnConfirm;
	Episode ep;
	
	public StoryGirlState(GameView gameView, Episode ep){
		
		m_gameview = gameView;
		situationArray = new ArrayList<BasicGirl>();
		
		this.ep = ep;
		initGirlEpisode(ep);		
	}
	
	// 에피소드 초기화
	protected void initGirlEpisode(Episode ep){		
		
		AssetFileDescriptor afd;
		try{
			if(ep.getBgSound() != null){			
				afd = MoeManager.mCurContext.getAssets().openFd("voice/" + ep.getBgSound());
				backgroundMp = new MediaPlayer();
				backgroundMp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());	
				backgroundMp.setLooping(true);
				backgroundMp.prepare();
				backgroundMp.start();
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		
		ArrayList<Comment> comments = ep.getComment();
		for(Comment cmt : comments){
			BasicGirl character = new BasicGirl(cmt);
			character.loadResource();
			situationArray.add(character);
		}
		
		if(situationArray.size() > 0){
			lastCharatcer = situationArray.get(situationArray.size() - 1);
		}
		character = situationArray.get(curStateIdx);
		changeCharacter(character);
	}
	
	public boolean moveNextState(){
		if(curStateIdx + 1 == situationArray.size()){
			return false;
		}
		
		// 세희가 먼저 끊어 버리는경우
		BasicGirl temp = situationArray.get(curStateIdx + 1);
		if("exit".equals(temp.chInfo.getVoice()) ){
			exitEpisode();
			return false;
		}
		
		character.stopVoice();
		character = situationArray.get(++curStateIdx);		
		changeCharacter(character);
		return true;
	}
	
	protected void changeCharacter(BasicGirl chr){
		chr.init();
		this.character = chr;
		
	}
	
	
	@Override
	public void Init() {
		// TODO Auto-generated method stub

		MoeManager.addImg("talk_next_btn.png");
				
		btnConfirm = new BitmapObj(
				MoeManager.getImg("talk_next_btn.png"),
				(int)(MoeManager.screenWidth - MoeManager.getImg("talk_next_btn.png").getWidth() - 10),
				(int)(MoeManager.screenHeight - MoeManager.getImg("talk_next_btn.png").getHeight() - 10),
				character.talkDesRect
				);
		
	}

	@Override
	public void Destroy() {
		// TODO Auto-generated method stub
		for(BasicGirl gl : situationArray){
			gl.destory();
		}
		if(backgroundMp != null){
			backgroundMp.stop();
		}
	}

	@Override
	public void Update() {
		// TODO Auto-generated method stub
		long GameTime = System.currentTimeMillis();
		character.Update(GameTime);
	}

	@Override
	public void Render(Canvas canvas) {
		// TODO Auto-generated method stub
		character.draw(canvas);
		
		if(character != null && character.equals(lastCharatcer)){			
			btnConfirm.draw(canvas);
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		
		MyUtil.touchMe(null, event, new ImageTouch() {
			
			@Override
			public void touch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				if(btnConfirm.isBoundary(event) && (character != null && character.equals(lastCharatcer))){
					exitEpisode();
				}
			}
		});

		
		return false;
	}
	
	public void exitEpisode(){
		if("ep30".equals(ep.getId())){
			ScheduleManager sm = new ScheduleManager((Activity)MoeManager.mCurContext);
			sm.episodeDone();
			
			MoeManager.clearImages();
			
			MoeManager.setWhatWillIdo(MoeManager.DO_ENDING); // 메인에서 모에를 호출함.
			Intent intent = new Intent(MoeManager.mCurContext, MoeActivity.class);
			
			((Activity)MoeManager.mCurContext).finish();
			MoeManager.mCurContext.startActivity(intent);
			
		}else{		
			// 에피소드가 끝남
			ScheduleManager sm = new ScheduleManager((Activity)MoeManager.mCurContext);
			sm.episodeDone();
			
			Intent iAlarm = new Intent(MoeManager.mCurContext, MainActivity.class);
			iAlarm.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			MoeManager.mCurContext.startActivity(iAlarm);
			
			((Activity)MoeManager.mCurContext).finish();
			MoeManager.setWhatWillIdo(MoeManager.DO_NOTHING);
		}
	}

}
